package com.cjgl.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cjgl.common.BaseResponse;
import com.cjgl.common.ErrorCode;
import com.cjgl.common.ResultUtils;
import com.cjgl.exception.BusinessException;
import com.cjgl.model.dto.course.SelectByCourseNoOrCourseNameRequest;
import com.cjgl.model.entity.TblCourses;
import com.cjgl.service.TblCoursesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zhangyuwu
 */
@RestController
@RequestMapping("/adminManageCourseInfos")
public class AdminManageCourseInfo {

	@Resource
	TblCoursesService coursesService;

	/**
	 *  courseNo courseName courseTime(课时) courseScore(学分)
	 * @param courseNoOrCourseNameRequest
	 * @param request
	 * @return
	 */
	@PostMapping
	public BaseResponse<Object> getCourseInfo(@RequestBody SelectByCourseNoOrCourseNameRequest courseNoOrCourseNameRequest, HttpServletRequest request) {
		String courseNo = courseNoOrCourseNameRequest.getCourseNo() == null ? "" : courseNoOrCourseNameRequest.getCourseNo();
		String courseName = courseNoOrCourseNameRequest.getCourseName() == null ? "" : courseNoOrCourseNameRequest.getCourseName();
		Integer courseTime = courseNoOrCourseNameRequest.getCourseTime() == null ? 0 : courseNoOrCourseNameRequest.getCourseTime();
		Integer courseScore = courseNoOrCourseNameRequest.getCourseScore() == null ? 0 : courseNoOrCourseNameRequest.getCourseScore();


		LambdaQueryWrapper<TblCourses> courseQueryWrapper = new LambdaQueryWrapper<>();
		courseQueryWrapper
				.like(!StringUtils.isEmpty(courseNo), TblCourses::getCourseNo, courseNo)
				.like(!StringUtils.isEmpty(courseName), TblCourses::getCourseName, courseName)
				.like(courseTime != 0, TblCourses::getCourseNo, courseNo)
				.like(courseScore != 0, TblCourses::getCourseNo, courseNo);
		List<TblCourses> tblCourses = coursesService.getBaseMapper().selectList(courseQueryWrapper);

		return ResultUtils.success(tblCourses);
	}

	@PostMapping("/add/course")
	public BaseResponse<Object> addCourse(@RequestBody TblCourses tblCourses){
		if (tblCourses == null){
			throw new BusinessException(ErrorCode.PARAMS_ERROR);
		}
		boolean save = coursesService.save(tblCourses);
		return ResultUtils.success(save);
	}

}
